<template>
  <EnteringForm
    ref="enteringForm"
    v-bind="dialog"
		width="60%"
    @close="close"
    @submit="submit"
  >
    <el-form ref="formRef" :model="queryParams" :rules="rules" label-width="120px">
      <el-row type="flex" style="flex-wrap: wrap">
        <!-- <el-col :xs="24" :sm="12" :lg="12">
          <el-form-item label="所属店铺" prop="sellerId">
            <el-select
							v-model="queryParams.sellerId"
							placeholder="请选择"
							style="width: 100%"
						>
							<el-option :label="item.name" :value="item.id" v-for="item in shopList" :key="item.id"/>
						</el-select>
          </el-form-item>
        </el-col> -->
				<el-col :xs="24" :sm="12" :lg="12">
          <el-form-item label="商品名称:" prop="name">
            <el-input
              v-model.trim="queryParams.name"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="成本价:" prop="cost">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="2"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.cost"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="售价:" prop="price">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="2"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.price"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="划线价:" prop="marketPrice">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="2"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.marketPrice"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="销量:" prop="sellCount">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="0"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.sellCount"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="排序:" prop="sort">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="0"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.sort"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col>
        <!-- <el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="赠送积分:" prop="integral">
						<el-input-number
							style="width:100%"
							controls-position="right" 
							:precision="0"
							:min="0"
							:max="99999999"
							v-model.trim="queryParams.integral"
							placeholder="请输入"
							clearable
						/>
					</el-form-item>
				</el-col> -->
				<el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="上架状态:" prop="enable">
						<el-radio-group v-model="queryParams.enable">
							<el-radio :label="1">上架</el-radio>
							<el-radio :label="0">下架</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="12" :lg="12">
					<el-form-item label="是否推荐:" prop="recommend">
						<el-radio-group v-model="queryParams.recommend">
							<el-radio :label="1">推荐</el-radio>
							<el-radio :label="0">不推荐</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
        <el-col :xs="24" :sm="24" :lg="24">
					<!-- 11--{{ queryParams.cover }} -->
          <el-form-item label="商品封面图:" prop="cover">
            <ImageUpload
              v-model="queryParams.cover"
              :limit="1"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="24">
          <!-- 22--{{ queryParams.images }} -->
          <el-form-item label="商品轮播图:" prop="images">
            <ImageUpload
              v-model="queryParams.images"
              :limit="6"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :lg="24">
          <el-form-item label="商品详情图:" prop="intro">
            <ImageUpload
              v-model="queryParams.intro"
              :limit="6"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </EnteringForm>
</template>

<script setup>
import { getCurrentInstance, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
// import { 
//   listPage as shopListApi  //店铺列表
// } from '@/api/operates/shopList/index'
import { info, add, edit, changeStatusApi, changeRecommendApi } from '@/api/goods/integrail.js'

const { proxy } = getCurrentInstance()
const emit = defineEmits(["success"]);
const dialogType = ref('')
const shopList = ref([])
const dialog = reactive({
  visible: false,
  title: "",
});
const rules = reactive({
  // sellerId: [{ required: true, message: "请选择", trigger: "change" }],
  name: [{ required: true, message: "请输入", trigger: "blur" }],
  cost: [{ required: true, message: "请输入", trigger: "blur" }],
  price: [{ required: true, message: "请输入", trigger: "blur" }],
  marketPrice: [{ required: true, message: "请输入", trigger: "blur" }],
  sellCount: [{ required: true, message: "请输入", trigger: "blur" }],
  sort: [{ required: true, message: "请输入", trigger: "blur" }],
  integral: [{ required: true, message: "请输入", trigger: "blur" }],
  cover: [{ required: true, message: "请上传", trigger: "blur" }],
  images: [{ required: true, message: "请上传", trigger: "blur" }],
  intro: [{ required: true, message: "请上传", trigger: "blur" }],
  enable: [{ required: true, message: "请选择", trigger: "change" }],
  recommend: [{ required: true, message: "请选择", trigger: "change" }],

});
const queryParams = ref({
	enable: 1,
	recommend: 0,
})

function init(type, record){
  // getShopList()
	dialogType.value = type
	dialogType.value=='add' ? dialog.title='新增' : dialog.title='编辑'
	dialog.visible = true
	// 如果是编辑，回显数据
	if(dialogType.value=='edit'){
		queryParams.value = { ...record };
	}
};

// function getShopList(){
//   shopListApi({}).then((res)=>{
//     shopList.value = res.data.list
//   })
// }
function close(){
  dialog.visible = false;
  proxy.$refs.formRef.resetFields();
	queryParams.value = {}
};
function submit() {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      let query = {
        ...queryParams.value,
        // cover:'test',
        // intro:'test',
        // images:'test'
      }
      if(dialogType.value=='add'){
				add(query).then((res) => {
					if (res.code == 200) {
						ElMessage.success("新增成功");
						close();
						emit("success",'add');
					} else {
						ElMessage.error(res.message || "新增失败");
					}
				});
			} else {
				edit(query).then((res) => {
					if (res.code == 200) {
						ElMessage.success("编辑成功");
						close();
						emit("success",'edit');
					} else {
						ElMessage.error(res.message || "编辑失败");
					}
				});
			}
    }
  });
}
defineExpose({ init });
</script>

<style scoped>
.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}
</style>
